import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import welcome from '../components/welcome.vue'
import Users from '../components/User/Users.vue'
import NokiaShip from '../components/Nokia/Ship.vue'
import Rights from '../components/Rights/Right.vue'
import AmazonPOInfo from '../components/Amazon/AmazonPOInfo.vue'
import AmazonPNInfo from '../components/Amazon/AmazonPNInfo.vue'
import SamplingRate from '../components/ProductManage/SamplingRate.vue'
import NokiaLabelIssue from '../components/Nokia/LabelIssueCheckInAndOut.vue'
import SolderPasterSpecialBuy from '../components/SolderPasterManage/SolderPasteSpecialBuy.vue'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [{
    path: '/',
    redirect: '/login',
    meta: {
      title: '登录'
    }
  }, {
    path: '/login',
    component: Login,
    meta: {
      title: '登录'
    }
  }, {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [{
      path: '/welcome',
      component: welcome,
      meta: {
        title: '首页'
      }
    }, {
      path: '/Users',
      component: Users,
      meta: {
        title: '用户管理'
      }
    },
    {
      path: '/Rights',
      component: Rights,
      meta: {
        title: '权限列表'
      }
    },
    {
      path: '/Ship',
      component: NokiaShip,
      meta: {
        title: 'Nokia出货报表'
      }
    }, {
      path: '/POInfoMaintain',
      component: AmazonPOInfo,
      meta: {
        title: 'Amazon PO信息维护'
      }
    }, {
      path: '/PNInfoMaintain',
      component: AmazonPNInfo,
      meta: {
        title: 'Amazon PN信息维护'
      }
    }, {
      path: '/SamplingRate',
      component: SamplingRate,
      meta: {
        title: '抽检比例维护'
      }
    },
    {
      path: '/LabelCheckInAndOut',
      component: NokiaLabelIssue,
      meta: {
        title: 'Nokia标签不良管理'
      }
    },
    {
      path: '/SolderPasteSpecialBuy',
      component: SolderPasterSpecialBuy,
      meta: {
        title: '锡膏特采申请'
      }
    }
    ],
    meta: {
      title: '首页'
    }
  }],
  mode: 'hash'
})
// 解决elementui重复点击Avoided redundant navigation to current location
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (location) {
  return originalPush.call(this, location).catch(err => err)
}

// 挂载路由导航筛选
router.beforeEach((to, from, next) => {
  // to表示将要访问的路径 from表示从哪个路径条转,next函数，表示放行 next(),next('login')
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if (to.path === '/login') {
    return next()
  } else {
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) {
      return next('/login')
    } else {
      return next()
    }
  }
})

export default router
